/**
 @Name：layui.blog 闲言轻博客模块
 @Author：徐志文
 @Alter：橘子俊 364666827@qq.com
 @Site：https://store.hisiphp.com
 @License：MIT
 @Site：http://www.layui.com/template/xianyan/
 */
layui.define(['element', 'layer','laypage','jquery','laytpl'],function(exports){
  var element = layui.element
      ,layer = layui.layer
      ,laypage = layui.laypage
      ,$ = layui.jquery
      ,laytpl = layui.laytpl;

    // 导航显示隐藏
    $("#mobile-nav").on('click', function(){
        $("#pop-nav").toggle();
    });
    // 分类滚动
    $(document).scroll(function() {
        var scrollTop = $(document).scrollTop();
        if (scrollTop < 118) {
            $('#category').css({top: (120-scrollTop)+'px'});
        } else if (scrollTop > 118) {
            $('#category').css({top: '0px'});
        }
    });

    // 评论的特效
    (function ($) {
        $.extend({
            tipsBox: function (options) {
                options = $.extend({
                    obj: null,  //jq对象，要在那个html标签上显示
                    str: "+1",  //字符串，要显示的内容;也可传html，如: "<b style=''>+1</b>"
                    startSize: "12px",  //动画开始的文字大小
                    endSize: "30px",    //动画结束的文字大小
                    interval: 600,  //动画时间间隔
                    color: "red",    //文字颜色
                    callback: function () { }    //回调函数
                }, options);

                $("body").append("<span class='num'>" + options.str + "</span>");

                var box = $(".num");
                var left = options.obj.offset().left + options.obj.width() / 2;
                var top = options.obj.offset().top - 10;
                box.css({
                    "position": "absolute",
                    "left": left + "px",
                    "top": top + "px",
                    "z-index": 9999,
                    "font-size": options.startSize,
                    "line-height": options.endSize,
                    "color": options.color
                });
                box.animate({
                    "font-size": options.endSize,
                    "opacity": "0",
                    "top": top - parseInt(options.endSize) + "px"
                }, options.interval, function () {
                    box.remove();
                    options.callback();
                });
            }
        });
      
        $(document).on('click', ".like", function() {
            var that = $(this);
            if( typeof(that.attr('data-url')) == 'undefined' ) {
                return false;
            }
            if(!(that.hasClass("layblog-this"))) {
                this.text = '已赞';
                that.addClass('layblog-this');
                $.post(that.attr('data-url'), function(res){
                    if (res.code == 0) {
                        $.tipsBox({
                            obj: that,
                            str: '+0',
                            callback: function () {}
                        });
                    } else {
                        $.tipsBox({
                            obj: that,
                            str: "+1",
                            callback: function () {}
                        });
                        if( typeof(that.attr('data-like')) != 'undefined' ) {
                            that.find('em').text(parseInt(that.attr('data-like')) + 1);
                        }
                    }
                });
            } 
        });

        // 评论加载
        if (typeof(msgGetUrl) == 'string') {
            var guestNick = window.localStorage.getItem("blog_guest_nick");
            var guestEmail = window.localStorage.getItem("blog_guest_email");
            if (guestNick) {
                $('#blog-guest-nick').val(guestNick)
            }
            if (guestEmail) {
                $('#blog-guest-nick').val(guestEmail)
            }

            var curPage = location.hash.replace('#!page=', '');
            $.get(msgGetUrl, {page: curPage}, function(res) {
                if (res.count <= 0) {
                    $('#LAY-msg-box').html('');
                    return false;
                }
                var view = $('#LAY-msg-tpl').html();
                laytpl(view).render(res.data, function(html) {
                    $('#LAY-msg-box').html(html);
                });
                if (res.count > res.limit) {
                    laypage.render({
                        elem: 'LAY-msg-pages'
                        ,limit: res.limit
                        ,count: res.count
                        ,theme: '#1e9fff'
                        ,curr: curPage
                        ,hash: 'page'
                        ,jump: function(obj, first) {
                            if (!first) {
                                $.get(msgGetUrl, {page: obj.curr}, function(res) {
                                    laytpl(view).render(res.data, function(html) {
                                      $('#LAY-msg-box').html(html);
                                    });
                                }, 'json');
                            }
                        }
                    });
                }
            }, 'json');

            $('#comment-form').on('submit', function(){
                var form = $(this)
                    , elemCont = form.find('textarea[name="content"]')
                    , content = elemCont.val()
                    , elemNick = form.find('input[name="nick"]')
                    , elemEmail = form.find('input[name="email"]')
                    , nick = '匿名';
                if (elemNick[0] && elemNick.val().replace(/\s/g, '') != '') {
                    nick = elemNick.val();
                    window.localStorage.setItem("blog_guest_nick", nick);
                }
                if (elemEmail[0] && elemEmail.val().replace(/\s/g, '') != '') {
                    window.localStorage.setItem("blog_guest_email", elemEmail.val());
                }
                if (content.replace(/\s/g, '') == '') {
                    layer.msg('请先输入留言');
                    elemCont.focus();
                    return false;
                }
                $.post(form.attr('action'), form.serialize(), function(res) {
                    if (res.code == 0) {
                        layer.msg(res.msg);
                    } else {
                        var data = [{nick: nick, like: 0, content: content}];
                        var view = $('#LAY-msg-tpl').html();
                        laytpl(view).render(data, function(html){
                            $('#LAY-msg-box').prepend(html);
                            elemCont.val('');
                            layer.msg(res.msg);
                        });
                    }
                }, 'json');

                return false;
            })
        }
    })($);

    // 图片遮罩
    var layerphotos = document.getElementsByClassName('layer-photos-demo');
    for(var i = 1;i <= layerphotos.length;i++){
        layer.photos({photos: ".layer-photos-demo"+i+"", anim: 0}); 
    }

    //输出test接口
    exports('blog', {}); 
});  
